// Pipit 加密算法模块
#include "pipit_stdlib.h"
#include <stdlib.h>
#include <string.h>

// MD5哈希（简化版本）
typedef struct {
    unsigned int state[4];
    unsigned int count[2];
    unsigned char buffer[64];
} pipit_md5_ctx_t;

// SHA256哈希
typedef struct {
    unsigned int state[8];
    unsigned long long count;
    unsigned char buffer[64];
} pipit_sha256_ctx_t;

// Base64编码
typedef struct {
    char* data;
    size_t length;
} pipit_base64_t;

// 简单的XOR加密
char* pipit_crypto_xor_encrypt(const char* text, const char* key) {
    if (!text || !key) return NULL;
    
    size_t text_len = strlen(text);
    size_t key_len = strlen(key);
    char* result = malloc(text_len + 1);
    
    for (size_t i = 0; i < text_len; i++) {
        result[i] = text[i] ^ key[i % key_len];
    }
    result[text_len] = '\0';
    return result;
}

// 凯撒密码加密
char* pipit_crypto_caesar_encrypt(const char* text, int shift) {
    if (!text) return NULL;
    
    size_t len = strlen(text);
    char* result = malloc(len + 1);
    
    for (size_t i = 0; i < len; i++) {
        char c = text[i];
        if (c >= 'a' && c <= 'z') {
            result[i] = 'a' + (c - 'a' + shift) % 26;
        } else if (c >= 'A' && c <= 'Z') {
            result[i] = 'A' + (c - 'A' + shift) % 26;
        } else {
            result[i] = c;
        }
    }
    result[len] = '\0';
    return result;
}

// 简单的校验和计算
unsigned int pipit_crypto_checksum(const char* data, size_t length) {
    unsigned int sum = 0;
    for (size_t i = 0; i < length; i++) {
        sum = (sum << 4) + (unsigned char)data[i];
        unsigned int high = sum & 0xF0000000;
        if (high) {
            sum ^= high >> 24;
            sum &= ~high;
        }
    }
    return sum;
}

// 生成随机字节
void pipit_crypto_random_bytes(unsigned char* buffer, size_t length) {
    static unsigned int seed = 12345;
    for (size_t i = 0; i < length; i++) {
        seed = (seed * 1103515245 + 12345) & 0x7fffffff;
        buffer[i] = (unsigned char)(seed % 256);
    }
}

// Base64编码实现
static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char* pipit_crypto_base64_encode(const unsigned char* data, size_t length) {
    if (!data) return NULL;
    
    size_t encoded_length = 4 * ((length + 2) / 3);
    char* encoded_data = malloc(encoded_length + 1);
    
    size_t i, j;
    for (i = 0, j = 0; i < length;) {
        unsigned int a = i < length ? (unsigned char)data[i++] : 0;
        unsigned int b = i < length ? (unsigned char)data[i++] : 0;
        unsigned int c = i < length ? (unsigned char)data[i++] : 0;
        
        unsigned int triple = (a << 16) | (b << 8) | c;
        
        encoded_data[j++] = base64_chars[(triple >> 18) & 0x3F];
        encoded_data[j++] = base64_chars[(triple >> 12) & 0x3F];
        encoded_data[j++] = base64_chars[(triple >> 6) & 0x3F];
        encoded_data[j++] = base64_chars[triple & 0x3F];
    }
    
    // 填充
    for (i = 0; i < (3 - length % 3) % 3; i++) {
        encoded_data[encoded_length - 1 - i] = '=';
    }
    
    encoded_data[encoded_length] = '\0';
    return encoded_data;
}

// 简单的哈希表实现
typedef struct {
    char* key;
    char* value;
} pipit_crypto_entry_t;

typedef struct {
    pipit_crypto_entry_t* entries;
    size_t count;
    size_t capacity;
} pipit_crypto_map_t;

pipit_crypto_map_t* pipit_crypto_map_create() {
    pipit_crypto_map_t* map = malloc(sizeof(pipit_crypto_map_t));
    map->capacity = 16;
    map->count = 0;
    map->entries = calloc(map->capacity, sizeof(pipit_crypto_entry_t));
    return map;
}

void pipit_crypto_map_set(pipit_crypto_map_t* map, const char* key, const char* value) {
    if (map->count >= map->capacity * 0.7) {
        // 扩容逻辑（简化版）
        map->capacity *= 2;
        map->entries = realloc(map->entries, map->capacity * sizeof(pipit_crypto_entry_t));
    }
    
    map->entries[map->count].key = strdup(key);
    map->entries[map->count].value = strdup(value);
    map->count++;
}

const char* pipit_crypto_map_get(pipit_crypto_map_t* map, const char* key) {
    for (size_t i = 0; i < map->count; i++) {
        if (strcmp(map->entries[i].key, key) == 0) {
            return map->entries[i].value;
        }
    }
    return NULL;
}

// 消息认证码（简化版HMAC）
char* pipit_crypto_hmac(const char* message, const char* key) {
    if (!message || !key) return NULL;
    
    size_t result_size = strlen(message) + strlen(key) + 32;
    char* result = malloc(result_size);
    
    // 简单的HMAC实现
    sprintf(result, "HMAC-%s-%s", message, key);
    
    return result;
}

// 密码强度检查
int pipit_crypto_password_strength(const char* password) {
    if (!password) return 0;
    
    int score = 0;
    size_t len = strlen(password);
    
    if (len >= 8) score += 20;
    if (len >= 12) score += 20;
    
    int has_lower = 0, has_upper = 0, has_digit = 0, has_special = 0;
    for (size_t i = 0; i < len; i++) {
        if (islower(password[i])) has_lower = 1;
        else if (isupper(password[i])) has_upper = 1;
        else if (isdigit(password[i])) has_digit = 1;
        else has_special = 1;
    }
    
    score += (has_lower + has_upper + has_digit + has_special) * 15;
    
    return score > 100 ? 100 : score;
}